package cn.kx.flutter_kst_plugin;

import android.app.Activity;
import android.content.Intent;

import androidx.annotation.NonNull;

import io.flutter.Log;
import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.embedding.engine.plugins.activity.ActivityAware;
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.PluginRegistry.Registrar;

import cn.kuaishang.kssdk.KSConfig;
import cn.kuaishang.kssdk.util.KSIntentBuilder;
import cn.kuaishang.listener.KsInitListener;

/** FlutterKstPlugin */
public class FlutterKstPlugin implements FlutterPlugin, MethodCallHandler , ActivityAware {

  private MethodChannel channel;
  private Activity activity;
  @Override
  public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
    channel = new MethodChannel(flutterPluginBinding.getFlutterEngine().getDartExecutor(), "flutter_kst_plugin");
    channel.setMethodCallHandler(this);
  }


  public static void registerWith(Registrar registrar) {
    final MethodChannel channel = new MethodChannel(registrar.messenger(), "flutter_kst_plugin");
    channel.setMethodCallHandler(new FlutterKstPlugin());
  }

  @Override
  public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
    if (call.method.equals("getPlatformVersion")) {
      result.success("Android " + android.os.Build.VERSION.RELEASE);
    }else if (call.method.equals("initKST")){
      String compId = call.argument("compId");
      String key = call.argument("key");
      initKST(key,compId, result);
    }else if (call.method.equals("openKST")){
      openKST();
    }else if (call.method.equals("closeKST")){
      closeKST();
    }  else {
      result.notImplemented();
    }
  }

  @Override
  public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
    channel.setMethodCallHandler(null);
  }

  @Override
  public void onAttachedToActivity(@NonNull ActivityPluginBinding binding) {
    this.activity = binding.getActivity();

  }

  @Override
  public void onDetachedFromActivityForConfigChanges() {

  }

  @Override
  public void onReattachedToActivityForConfigChanges(@NonNull ActivityPluginBinding binding) {

  }

  @Override
  public void onDetachedFromActivity() {

  }

  public void initKST(String key, String compId, @NonNull final Result result) {

    KSConfig.init(activity, key,compId,new
            KsInitListener(){
              @Override
              public void onSuccess() {
                //TODO 初始化成功
                result.success("true");
              }
              @Override
              public void onError(int code, String message) {
                //TODO 初始化失败
                result.success("false");
              }
            });
  }


  public void openKST() {
    Intent intent = new KSIntentBuilder(activity).build();
    activity.startActivity(intent);
  }

  public void closeKST() {
    KSConfig.closeDialog(activity);
  }

}

